package com.example.demo.controller;

import com.example.demo.entity.Comment;
import com.example.demo.entity.Idea;
import com.example.demo.entity.Open;
import com.example.demo.entity.Status;
import com.example.demo.mapper.CommentMapper;
import com.example.demo.mapper.OpenMapper;
import com.example.demo.mapper.StatusMapper;
import com.example.demo.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CommentController {

    @Autowired
    private StatusMapper statusMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private OpenMapper openMapper;


    //总的说说
    @RequestMapping("/Allidea")
    public List<Status> Allidea (){
        List<Status> list=statusMapper.selectAll("o7xPK5Q9T3RsApyAZ1s5pX2ISpKs");
        return list;
    }


    //添加评论
    @RequestMapping("/addComment")
    public Comment addComment(String openid,int ideaid,String contentdetails){
        Comment comment =new Comment();
        DateUtils dateUtils=new DateUtils();
        if (openid!=null&&ideaid!=-1&&contentdetails!=null){
            comment.setOpenid(openid);
            comment.setIdeaid(ideaid);
            comment.setContentdetails(contentdetails);
            comment.setCommentdata(dateUtils.getFormatDate());
            comment.setOpen(openMapper.findOpenid(openid));


            int i=commentMapper.InsertComment(comment);

            if(i==1){
                return comment;
            }else {
                return null;
            }
        }else {
            return null;
        }
    }



    //通过说说寻找评论
    @RequestMapping("/findCommentByIdeaid")
    public List<Comment> findCommentByIdeaid(int ideaid){
        List<Comment> commentList=commentMapper.findCommentByIdeaid(ideaid);
        return commentList;
    }


    //删除评论
    @RequestMapping("/delectComment")
    public boolean delectComment(int commentid){
        int delectComment =commentMapper.DeleteComment(commentid);
        return true;
    }
}
